# 请下载python3、lxml后再使用 用pip install lxml
import requests
import os
from lxml import etree
import json
import execise_proxy_ip
import random
# TODO 常量保存到文件中
urlIndex = "https://www.17k.com"
urlBookCatalogue = "list"
urlBookRefer = "book"
urlLogin = "https://passport.17k.com/ck/user/login"
urlBookcase = "https://user.17k.com/ck/author/shelf?page=1&appKey=2406394919"
# 用户登录
loginName = "你的账号"
password = "你的密码"
session = requests.session()
session.post(urlLogin, data={
    "loginName": loginName,
    "password": password
})
print(session.cookies)
somebookdict = {}
# 获取代理IPs
ips = execise_proxy_ip.get_ips()


# TODO 书架未设置多页查找，可for循环再判code是否为200，若为200就获取书架信息，若非200就退出方法
# 获取书架中书籍信息
# 书架网址
def get_bookcase(ips):
    res = session.get(urlBookcase,
                      proxies={
                          "http": random.choice(ips)
                      })
    dictBookcase = eval(res.text)
    for bookItem in dictBookcase["data"]:
        bookdict = {}
        bookId = bookItem["bookId"]
        bookName = bookItem["bookName"]
        bookdict["bookName"] = bookName
        bookdict["bookId"] = bookId
        somebookdict[bookId] = bookdict
    return somebookdict


# 通过书籍ID获取书籍的目录信息
def get_catalogue(somebookdict, ips):
    for bookId, bookItem in somebookdict.items():
        bookCatalogueUrl = urlIndex + "/" + urlBookCatalogue + "/" + str(bookId) + ".html"
        userReferer = urlIndex + "/" + urlBookRefer + "/" + str(bookId) + ".html"
        res = session.get(bookCatalogueUrl,
                          proxies={
                              "http": random.choice(ips)
                          })
        res.encoding = "utf8"
        selector = etree.HTML(res.text)
        bookCatalogue = selector.xpath('//dl[@class="Volume"]/dd/a')
        bookSection = {}
        for linkE in bookCatalogue:
            # 获取章节名称
            sectionName = linkE.xpath('./span/text()')
            # 获取章节的内容网站
            href = linkE.xpath('@href')
            bookSection[sectionName[0].strip()] = href[0].strip()
            bookItem["bookSection"] = bookSection
        somebookdict[bookId] = bookItem
    return somebookdict


# 读取网页中书籍的内容
def get_content(somebookdict, ips):
    for bookId, bookSectionByBookId in somebookdict.items():
        bookName = bookSectionByBookId["bookName"]
        if not os.path.exists(bookName):
            os.makedirs(bookName)
        # print(bookId,bookSectionByBookId)
        for sectionName, href in bookSectionByBookId["bookSection"].items():
            bookContent = urlIndex + "/" + href
            resByContent = session.get(bookContent,
                                       proxies={
                                           "http": random.choice(ips)
                                       })
            # print(resByContent.encoding)
            resByContent.encoding = "utf8"
            selectorByContent = etree.HTML(resByContent.text)
            contentByBookId = selectorByContent.xpath('//div[@class="readAreaBox content"]/div[@class="p"]/p/text()')
            contents = ""
            for content in contentByBookId:
                contents += "\t" + content + "\n"
            with open(bookName + "/" + sectionName + ".txt", "w") as f:
                f.write(contents)
                # 测试可解开break的注释
            break


# 获取书架中书籍信息
somebookdict = get_bookcase(ips)
# 通过书籍ID获取书籍的目录信息
somebookdict = get_catalogue(somebookdict, ips)
# 读取网页中书籍的内容
get_content(somebookdict, ips)
